﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using GC2.ExceptionHandler;
using GC2.Entities.Admin;
using GC2.Bussines.BMAdmin;

public partial class UserControls_Articulo : GenericUserControl
{
    #region Propiedades

    public TextBox TxtIdArticulo
    {
        get { return txtIdArticulo; }
        set { txtIdArticulo = value; }
    }

    public TextBox TxtArticulo
    {
        get { return txtArticulo; }
        set { txtArticulo = value; }
    }

    public TextBox TxtStock
    {
        get { return txtStock; }
        set { txtStock = value; }
    }

    #endregion

    #region Controles

    #region Page_Load
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            SetearCultura();
            SetearConexion();
            if (!IsPostBack)
            {

            }
        }
        catch (Exception ex) { throw ex; }
    }
    #endregion

    #region btnBuscarArticulo_Click
    protected void btnBuscarArticulo_Click(object sender, EventArgs e)
    {
        try
        {
            txtIdArticulo.Text = "";
            txtArticulo.Text = "";
            txtStock.Text = "";
            BuscarArticulo();
        }
        catch (Exception ex) { throw ex; }
    }
    #endregion

    #endregion

    #region Metodos

    #region BuscarArticulo
    protected void BuscarArticulo()
    {
        try
        {
            List<Stock> listaStock = new List<Stock>();
            listaStock = (List<Stock>)Session["listaStock"];
            Stock articulo = new Stock();
            articulo.Id_Rubro = Convert.ToInt16(Rubros1.CboRubro.SelectedValue);
            articulo.Id_SubRubro = Convert.ToInt16(Rubros1.CboSubRubro.SelectedValue);
            if (txtIdArticulo.Text.Trim() != "")
            {
                articulo.Id_Articulo = Convert.ToInt16(txtIdArticulo.Text.Trim());
            }
            articulo.Articulo = txtArticulo.Text.Trim();
            Session["Articulo"] = "";
            bool encontrado = false;
            foreach (Stock item in listaStock)
            {
                if (item.Id_Articulo == articulo.Id_Articulo)
                {
                    if (item.Id_Rubro == articulo.Id_Rubro)
                    {
                        if (item.Id_SubRubro == articulo.Id_SubRubro)
                        {
                            txtIdArticulo.Text = item.Id_Articulo.ToString();
                            txtArticulo.Text = item.Articulo;
                            txtStock.Text = item.Saldo.ToString();
                            Session["Articulo"] = item;
                            encontrado = true;
                            break;
                        }
                    }
                }
                if (item.Articulo == articulo.Articulo)
                {
                    if (item.Id_Rubro == articulo.Id_Rubro)
                    {
                        if (item.Id_SubRubro == articulo.Id_SubRubro)
                        {
                            txtIdArticulo.Text = item.Id_Articulo.ToString();
                            txtArticulo.Text = item.Articulo;
                            txtStock.Text = item.Saldo.ToString();
                            Session["Articulo"] = item;
                            encontrado = true;
                            break;
                        }
                    }
                }
            }

            if (!encontrado)
            {
                txtIdArticulo.Text = "";
                txtArticulo.Text = "";
                #region dispararPopUp(hacePostBack)

                string scriptSession = @"
            <script language=""javascript"" type=""text/javascript"">
            <!--
                function mostrarArticulo()
                {
                    var stringURL = 'BuscarArticulo.aspx?pStation=0';
                    var oINPUTS = document.getElementsByTagName (""INPUT"");
                    for(var a=0;a<oINPUTS.length; a++)
                    {
                        //Validaciones para cuando el control fue arrastrado a un PopUP (Sin MasterPage)

                        if(oINPUTS[a].getAttribute(""id"") == 'ct100_DefaultContent_txtIdArticulo')
                        {
                            //alert('U1a: &IdArticulo = ' + oINPUTS[a].getAttribute(""value""));
                            stringURL = stringURL + '&Id_Articulo=' + oINPUTS[a].getAttribute(""value"");
                        }
                        if(oINPUTS[a].getAttribute(""id"") == 'ct100_DefaultContent_txtArticulo')
                        {  
                            //alert('U2a: &Concepto = ' + oINPUTS[a].getAttribute(""value""));
                            stringURL = stringURL + '&Articulo=' + oINPUTS[a].getAttribute(""value"");
                        }
                    }
                    // alert(stringURL);
                    var newWin = window.open(stringURL, ""popup"", ""menubar=no, resizable=no, scrollbars=yes, height=250, width=650, left=100, top=100"");
                }
                mostrarArticulo();
            //-->
            </script>
            ";

                Page.ClientScript.RegisterStartupScript(this.GetType(), "ScriptA", scriptSession);
                #endregion
            }

        }
        catch (Exception ex) { throw ex; }
    }
    #endregion

    #endregion

}